package com.voxel.sense.infomsg.module.user.personal;

import android.app.Activity;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.com.common.app.BaseFragment;
import com.example.com.common.factory.data.DataSource;
import com.example.com.common.utils.L;
import com.example.com.common.utils.NetworkUtils;
import com.example.com.common.utils.ToastUtil;
import com.example.com.common.view.dialog.DialogCallBack;
import com.example.com.common.view.dialog.IDialog;
import com.voxel.sense.infomsg.App;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.activity.MsgReminderActivity;
import com.voxel.sense.infomsg.module.map.location.SearchLocationMapActivity;
import com.voxel.sense.infomsg.module.map.warn.FirmWarnActivity;
import com.voxel.sense.infomsg.module.task.send.SendTaskActivity;
import com.voxel.sense.infomsg.module.task.send.SendTaskContract;
import com.voxel.sense.infomsg.module.user.audit.AuditUserActivity;
import com.voxel.sense.infomsg.module.user.personal.details.UserDetailsActivity;
import com.voxel.sense.infomsg.utils.GlideUtils;
import com.voxel.sense.infomsg.widget.AppVersionManager;
import com.voxel.sense.infomsg.widget.map.MapInfoPopWindows;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cyy.example.com.facotry.Factory;
import cyy.example.com.facotry.constants.CommonConstants;
import cyy.example.com.facotry.model.db.MessageModle;
import cyy.example.com.facotry.net.helper.DbHelper;
import cyy.example.com.facotry.share.SPEngine;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 负责构造一些全局的配置及变量
 * Created by chenyiyao on 2017/10/27.
 */

public class PersonFragment extends BaseFragment {
    @BindView(R.id.tv_name)
    TextView tv_name; //用户名
    @BindView(R.id.tv_phone)
    TextView tv_phone; //用户名
    @BindView(R.id.tv_version_up_tip)
    TextView tv_version_up_tip;//版本更新的tip
    @BindView(R.id.rl_check_version)
    RelativeLayout rl_check_version;//检查版本更新
    @BindView(R.id.iv_portrait)
    CircleImageView iv_portrait;//头像display
    //TODO:测试代码
    @BindView(R.id.ll_test)
    LinearLayout ll_test;//设置头像

    @Override
    protected int getContentLayoutId() {
        return R.layout.frag_person;
    }

    @Override
    protected void initData() {
        super.initData();
        SPEngine.UserInfo userInfo = SPEngine.getSPEngine().getUserInfo();

//            ll_test.setVisibility(View.VISIBLE);
        if (L.isDebug) {
            ll_test.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        SPEngine.UserInfo userInfo = SPEngine.getSPEngine().getUserInfo();
        String portrait = userInfo.getPortrait();
        if (!TextUtils.isEmpty(portrait)) {
            GlideUtils.GlideLoadPortrait(getContext(), portrait, iv_portrait);
        }

        //判断是否有新版本
        if (AppVersionManager.haveNewVersion()) {
            tv_version_up_tip.setVisibility(View.VISIBLE);
        } else {
            tv_version_up_tip.setVisibility(View.GONE);
        }

        String name = userInfo.getName();
        if (!TextUtils.isEmpty(name)) {
            tv_name.setText(userInfo.getUser_account() + "(" + name + ")");
        } else {
            tv_name.setText(userInfo.getUser_account());
        }
        tv_phone.setText(userInfo.getPhone());
    }

    @OnClick({R.id.tv_clean_cache, R.id.tv_check_version, R.id.tv_exit, R.id.tv_set_message, R.id.ll_test, R.id.cl_info_content})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_clean_cache://清楚缓存
                cleanCache();
                break;
            case R.id.tv_check_version:
                checkVersion();//检查版本更新
//                SendTaskActivity.show(getContext(), SendTaskContract.WARM_TASK);


                break;
            case R.id.tv_exit: //退出
//                SettingActivity.show(getContext());
                IDialog.getInstance().showChooseDialog(getContext(), "", "是否要退出账号", new DialogCallBack() {
                    @Override
                    public void onClick(int what) {
                        if (IDialog.YES == what) {
                            App.getInstance().logout(0);
                        }
                    }
                });
                break;
            case R.id.tv_set_message://设置消息
                //小米推送，设置别名
//                MiPushClient.setAlias(Factory.app(), SPEngine.getSPEngine().getUserInfo().getUser_id(), null);
                MsgReminderActivity.show(getContext());
//                FullScreenJPlayerActivity.˝show(getContext(),"http://hls.open.ys7.com/openlive/9a9a4978e0724836a52441a8850b71d7.m3u8");
                break;
            case R.id.cl_info_content://跳转我的信息界面
                UserDetailsActivity.show(getActivity());
                break;
            case R.id.ll_test:
//                RecordTraceActivity.show(getContext());
//                SearchLocationMapActivity.show((Activity) getContext());
                MapInfoPopWindows mapInfoPopWindows = new MapInfoPopWindows(getActivity());
                mapInfoPopWindows.show();
                break;
        }
    }

    private void checkVersion() {
        if (NetworkUtils.isConnected()) {
            showProgressDialog("");
//            NetHelper.checkAppVersion(this);
            AppVersionManager.checkVerson(true, new DataSource.Callback() {
                @Override
                public void onDataNotAvailable(String strRes, int reqId) {
                    dismissProgressDialog();
                }

                @Override
                public void onDataLoaded(Object data, int reqId) {
                    //管理类中已处理，这里可直接判断
                    dismissProgressDialog();
                    if (AppVersionManager.haveNewVersion()) {
                        tv_version_up_tip.setVisibility(View.VISIBLE);
                    } else {
                        ToastUtil.showToastLong(Factory.app(), "暂无更新信息");
                        tv_version_up_tip.setVisibility(View.GONE);
                    }
                }
            });
        } else {
            ToastUtil.showToastLong(getContext(), getString(R.string.network_unavailable));
        }
    }

    private void cleanCache() {
        IDialog.getInstance().showChooseDialog(getContext(), getString(R.string.warm_prompt), "是否清除缓存", new DialogCallBack() {
            @Override
            public void onClick(int what) {
                if (IDialog.YES == what) {
                    showProgressDialog("正在清除...");
                    App.getInstance().doCleanCache();
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            dismissProgressDialog();
                        }
                    }, 1000);
                }
            }
        });
    }

}
